package cn.hd01.job;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import cn.hd01.common.BaseConstant;
import cn.hd01.common.BaseConstant.CustomerType;
import cn.hd01.repository.entity.Customer;
import cn.hd01.repository.entity.TimingSms;
import cn.hd01.service.CustomerService;
import cn.hd01.service.TimingSmsService;
import cn.hd01.sms.SmsService;

/**
 * 定时发送短信提醒
 * @author Bill
 *
 */
@Component
public class TimingSmsJob {
	
	@Autowired
	private TimingSmsService timeingSmsService;
	
	@Autowired
	private SmsService smsService;
	
	@Autowired
	private CustomerService customerService;
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
	
	@Scheduled(cron="0 0 0/1 * * ? ")
	public void sendSms(){
		Date now = Calendar.getInstance().getTime();
		String nowStr = sdf.format(now);
		
		List<TimingSms> list = timeingSmsService.findByStatus(BaseConstant.Status.INVALID);
		if(!CollectionUtils.isEmpty(list)){
			for(TimingSms sms : list){
				if(sdf.format(sms.getSendTime()).equals(nowStr)){
					smsService.sendFixTimeMsg(sms.getPhoneNo(), new StringBuffer(sms.getCustomerName()).append(",").append(sms.getContent()).toString());
					timeingSmsService.updateStatus(BaseConstant.Status.VALID, sms.getId());
				}
			}
		}
	}
	
}
